Skip to content

Call the rate limiter many times in the same php code with different limits#3

Open
leonardoxc wants to merge 1 commit intoakirk:masterfrom
leonardoxc:master
Open

Call the rate limiter many times in the same php code with different limits#3
leonardoxc wants to merge 1 commit intoakirk:masterfrom
leonardoxc:master

Conversation

@leonardoxc
Copy link
Copy Markdown

With this branch we can do this:

$memcache = new Memcache;
$memcache->connect('127.0.0.1',11211);

$rateLimiter = new RateLimiter($memcache, $_SERVER["REMOTE_ADDR"]);
try {
    // 10 requests / minute
    $rateLimiter->limitRequestsInMinutes(10, 1);
} catch (RateExceededException $e) {
    header("HTTP/1.0 529 Too Many Requests");
    exit;
}

try {
    // 30 requests / 5 minute
    $rateLimiter->limitRequestsInMinutes(30, 5);
} catch (RateExceededException $e) {
    header("HTTP/1.0 529 Too Many Requests");
    exit;
}

etc....

Copy link
Copy Markdown
Owner

@akirk akirk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the mix of coding standards, I am not sure about the logic you introduce. Why the subtraction part, instead of simply not counting it in the first place, if it had been visited in the current request already? So instead of $keysvisited we could do a $already_counted and skip the increment part when that has already been done.

Comment thread ratelimiter.php
private $prefix, $memcache;
private $prefix, $memcache , $keysvisited;
// how long should we keep memcache entries
public $maxMinutes=10;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add whitespace around the =.

Comment thread ratelimiter.php
public function __construct(Memcache $memcache, $ip, $prefix = "rate") {
$this->memcache = $memcache;
if (!$memcache) {
echo "Problem connecting to memcache server";
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In accordance with the rest of the code, this should not output something but throw an exception.

Comment thread ratelimiter.php
exit;
}
$this->prefix = $prefix . $ip;
$keysvisited=array();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add whitespace around the =.

Comment thread ratelimiter.php
foreach ($this->getKeys($minutes) as $key) {
$requestsInCurrentMinute = $this->memcache->get($key);

// if the key is read for a second or third tim in the same
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

Comment thread ratelimiter.php
} else {
$this->memcache->increment($key, 1);
}
$this->keysvisited[$key]=1;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add whitespace around the =.

Comment thread ratelimiter.php
$this->keysvisited[$key]++;
}

echo " You already have $requests requests in $minutes min<BR>";
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library should not output anything. This text could be placed in the Exception description, though.

Comment thread ratelimiter.php
// php execution, we remove the previous additions so that the
// last call reports correct numbers
if ($this->keysvisited[$key]) {
$requestsInCurrentMinute-=$this->keysvisited[$key];
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add whitespace around the -=.

Comment thread ratelimiter.php
// if the key is read for a second or third tim in the same
// php execution, we remove the previous additions so that the
// last call reports correct numbers
if ($this->keysvisited[$key]) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be checked with an isset() or it will generate a warning.

Comment thread ratelimiter.php

class RateLimiter {
private $prefix, $memcache;
private $prefix, $memcache , $keysvisited;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace ahead of the comma.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants